package com.shop.cereshop.business.controller;

import com.shop.cereshop.business.alioss.page.Url;
import com.shop.cereshop.business.alioss.service.FileUploadService;
import com.shop.cereshop.business.dl.logic.service.UpLoadService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 */
@RestController
@RequestMapping("/file")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "UploadController")
@CrossOrigin
@Api(value = "文件上传", tags = "文件上传")
public class UploadController {

    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private UpLoadService upLoadService;

//    /**
//     * 文件上传
//     * @param file
//     */
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public Result<Url> upload(MultipartFile file) throws Exception{
        Result result=new Result();
        if(null != file){
            if(file.getOriginalFilename().contains("mp4")){
                //如果上传的视频,校验大小不能超过2M
                if(file.getSize()>2048*1024){
                    return new Result(CoReturnFormat.MP4_FILE_NOT_2M);
                }
            }
            String url=fileUploadService.uploadFile(file.getOriginalFilename(),file.getInputStream(),file.getSize());
            result=new Result(new Url(url));
        }
        return result;
    }


//    /**
//     * 文件上传
//     * @param file
//     */
//    @PostMapping("/upload")
//    @ApiOperation(value = "文件上传")
//    public Result<Url> upload(MultipartFile file) throws Exception{
//        Result result=new Result();
//        if(null != file){
//            if(file.getOriginalFilename().contains("mp4")){
//                //如果上传的视频,校验大小不能超过2M
//                if(file.getSize()>2048*1024){
//                    return new Result(CoReturnFormat.MP4_FILE_NOT_2M);
//                }
//            }
//            result=new Result(new Url(upLoadService.upLoad(file, "0")));
//        }
//        return result;
//    }
}
